package org.sgx.gwteditors.client.impl1.misc;

import java.util.List;

import org.sgx.gwteditors.client.editor.Editor;
import org.sgx.gwteditors.client.editor.error.EditorError;
import org.sgx.gwteditors.client.editor.error.EditorErrorManager;
import org.sgx.gwteditors.client.editor.event.EditorEventManager;
import org.sgx.gwteditors.client.editor.event.ValueChangeListener;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.EditorManager1;
import org.sgx.gwteditors.client.impl1.misc.richtexttoolbar.RichTextToolbar;

import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
/**
 * a Rich Text Editor that will handle HTML strings. 
 * @author sg
 *
 */
public class RichTextEditor extends VerticalPanel implements Editor<String>{

	private RichTextArea textArea;
	private RichTextToolbar toolbar;

	public RichTextEditor() {
		super();
		textArea = new RichTextArea();
		toolbar = new RichTextToolbar(textArea);
		add(toolbar);
		add(textArea); 
		this.getElement().getStyle().setProperty("border", "1px solid #ededed"); 
	}

	@Override
	public boolean isSimple() {
		return true;
	}

	@Override
	public String flush() {
		return textArea.getHTML(); 
		// no validation / errors
	}

	@Override
	public void load(String t) {
		textArea.setHTML(t); 
	}

	@Override
	public Widget getWidget() {
		return this;
	}

	@Override
	public void addValueChangeListener(ValueChangeListener<String> l) {
		EditorEventManager.<String> getInstance().addValueChangeListener(this, l);
	}

	@Override
	public void removeValueChangeListener(ValueChangeListener<String> l) {
		EditorEventManager.<String> getInstance().removeValueChangeListener(this, l);
	}

	@Override
	public List<EditorError<String>> getErrors() {
		return EditorErrorManager.<String> getInstance().getErrors(this);
	}

	@Override
	public String getEditorId() {
		return EditorManager1.getInstance().getEditorName(this);
	}

	@Override
	public String[] getEditorTypesSupported() {
		return new String[] { EditorFramework1.TYPE_STRING };
	}
}
